package com.yunfei.yikao.web.config;

import com.yunfei.yikao.web.dataobject.ZhangyunfeiYikao;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: yunfei
 * @Date: 19:45
 */
@Component
//拦截器
public class AuthHanderInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   if(handler instanceof HandlerMethod){
       HandlerMethod handlerMethod=(HandlerMethod) handler;
       NeedLogin needLogin = handlerMethod.getMethodAnnotation(NeedLogin.class);
       if (needLogin != null) {
           ZhangyunfeiYikao yikao=(ZhangyunfeiYikao) request.getSession().getAttribute("yikao");
           if (yikao == null) {
               response.sendRedirect("/login.html");
               return false;
           }

       }
   }
        return true;
    }
}
